<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h3{
            height: 500px;
        }
    </style>
</head>
<body>
    <h3>hhh</h3>
    <h3>hhh</h3>
    <h3>hhh</h3>
    <h3>hhh</h3>
    <h3>hhh</h3>
    <h3>hhh</h3>
    <h3>hhh</h3>

    <script>
        //节流与防抖的区别为，一直拖动也会在设置时间内执行一次

        function throttle(fn, delay){
            var valid = false;
            return function(){
                if(valid){
                    return true;
                }
                valid = true;
                setTimeout(function(){
                    fn();
                    valid = false;
                }, delay);
            }
        }
        //滚动事件
        window.onscroll = throttle(scrollHandle, 300);
        
        function scrollHandle(){
            var scrollTop = document.documentElement.scrollTop;
            console.log('滚动条位置'+scrollTop);
        }
    </script>
</body>
</html>